package org.oimwrapper.connectors;

import Thor.API.Security.*;
import Thor.API.Security.LoginHandler.*;

/**
 * The basis for all threads spawned from OIM tasks.
 */
public abstract class OIMTaskThreadBase extends Thread {
	
	/**
	 * The invoking task.
	 */
	private OIMTaskBase caller;
	
	/**
	 * The OIM session
	 */
	private LoginSession oimSession;
		
	/**
	 * Default Constructor.
	 * 
	 * @param Caller The invoking task.
	 */
	protected OIMTaskThreadBase(OIMTaskBase Caller) {
		caller = Caller;
		oimSession = null;
	}
	
	/**
	 * Called to set the OIM session information in the thread.
	 * 
	 * @param OimSession The OIM Session.
	 */
	protected final void setOimSession(LoginSession OimSession) {
		oimSession = OimSession;
	}

	/**
	 * Executes the thread.
	 */
	public final void run() {
		try {
			XLClientSecurityAssociation.setThreadLoginSession(oimSession);
			runOimThread();
		} catch (Exception e) {
			OIMExceptionHandler.handleException(caller.logger, e, getClass());
		}
		caller.completeThread(this);
	}

	/**
	 * Executes the OIM portion of the thread.
	 * 
	 * @exception Exception
	 */
	protected abstract void runOimThread() throws Exception;
}